#include <iostream>
using namespace std;
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

#if 0 /* 孤儿进程 */
int main()
{
    /* 创建子进程 */
    pid_t pid = fork();
    if (pid < 0)
    {
        perror("fork error:");
        /* 进程退出  */
        _exit(-1);
    }
    else if (pid == 0)
    {
        /* 子进程 */
        int cnt = 180;
        while (cnt--)
        {
            cout << "l am child process:" << getpid() << endl;
            sleep(1);
        }
    }
    else
    {
        /* 父进程 */
        cout << "l am parent process, plan to exit:" << "pid:" << getpid() << "ppid:" << getppid() << endl;
        _exit(0);
    }

    return 0;
}

#endif

#if 1 /* 僵尸进程 */
int main()
{
    /* 创建子进程 */
    pid_t pid = fork();
    if (pid < 0)
    {
        perror("fork error:");
        /* 进程退出  */
        _exit(-1);
    }
    else if (pid == 0)
    {
        cout << "l am child process, l plan to exit." << endl;
        _exit(0);
    }
    else
    {
        int cnt = 180;
        while (cnt--)
        {
            cout << "l am parent child." << endl;
            sleep(1);
        }
    }
    /* 回收资源 */
    wait(NULL);

    return 0;
}

#endif
